package com.example.normaldemo.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.example.normaldemo.dragVIew.Lot;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
 * 覆盖模式的SharePreference
 */
public class SharePreferencesUtils {
    private final static String SP_NAME = "sp_cache";
    private static SharedPreferences mPreferences;        // SharedPreferences的实例
    private static final String TAG = "SharePreferencesUtils";

    private static SharedPreferences getSp(Context context) {
        if (mPreferences == null) {
            mPreferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);//覆盖
        }
        return mPreferences;
    }

    /**
     * 通过SP获得boolean类型的数据，没有默认为false
     *
     * @param context : 上下文
     * @param key     : 存储的key
     * @return
     */
    public static boolean getBoolean(Context context, String key) {
        SharedPreferences sp = getSp(context);
        return sp.getBoolean(key, false);
    }

    /**
     * 通过SP获得boolean类型的数据，没有默认为false
     *
     * @param context  : 上下文
     * @param key      : 存储的key
     * @param defValue : 默认值
     * @return
     */
    public static boolean getBoolean(Context context, String key, boolean defValue) {
        SharedPreferences sp = getSp(context);
        return sp.getBoolean(key, defValue);
    }

    /**
     * 设置int的缓存数据
     *
     * @param context
     * @param key     :缓存对应的key
     * @param value   :缓存对应的值
     */
    public static void setBoolean(Context context, String key, boolean value) {
        SharedPreferences sp = getSp(context);
        SharedPreferences.Editor edit = sp.edit();// 获取编辑器
        edit.putBoolean(key, value);
        edit.commit();
    }

    /**
     * 缓存float
     *
     * @param context
     * @param key
     * @param defValue
     * @return
     */
    public static void setFloat(Context context, String key, float defValue) {
        SharedPreferences sp = getSp(context);
        SharedPreferences.Editor edit = sp.edit();// 获取编辑器
        edit.putFloat(key, defValue);
        edit.commit();
    }

    /**
     * 获取int型数据
     * @param context
     * @param key
     * @param defValue
     * @return
     */
    public static int getInt(Context context, String key, int defValue) {
        SharedPreferences sp = getSp(context);
        return sp.getInt(key, defValue);
    }

    /**
     * 获取string值
     * @param context
     * @param key
     * @param defValue
     * @return
     */
    public static String getString(Context context, String key, String defValue) {
        SharedPreferences sp = getSp(context);
        return sp.getString(key, defValue);
    }

    public static float getFloat(Context context, String key, float defValue) {
        SharedPreferences sp = getSp(context);
        return sp.getFloat(key, defValue);
    }

    public static long getLong(Context context, String key, long defValue) {
        SharedPreferences sp = getSp(context);
        return sp.getLong(key, 0);
    }

    /**
     * 储存数组
     */
    public static void setArrayData(Context context, String key, CharSequence text[]) {
        if (text == null || text.length == 0) {
            return;
        }
        SharedPreferences sp = getSp(context);
        SharedPreferences.Editor edit = sp.edit();// 获取编辑器
        Set set = new HashSet();
        for (int i = 0; i < text.length; i++) {
            set.add(text[i]);
        }
        edit.putStringSet(key, set);
        edit.commit();
    }

    /**
     * 获取数组
     */
    public static CharSequence[] getStringSet(Context context, String key, Set<String> defValue) {
        CharSequence sequence[] = new CharSequence[6];
        SharedPreferences sp = getSp(context);
        Set set = sp.getStringSet(key, defValue);
        if (set != null && set.size() > 0) {
            Iterator<Object> it = set.iterator();
            for (int i = 0; i < set.size(); i++) {
                sequence[i] = (CharSequence) it.next();
            }
            Arrays.sort(sequence);
        }
        return sequence;
    }


    /**
     * 保存list数据
     */
    public static void setArrayList(Context context, String key, ArrayList<Lot> list) {
        if (list == null || list.size() == 0) {
            return;
        }
        Log.d(TAG, list.size() + "");
        SharedPreferences sp = getSp(context);
        SharedPreferences.Editor edit = sp.edit();// 获取编辑器
        int size = list.size();
        setInt(context, "listSize", size);//保存list的大小
        for (int i = 0; i < list.size(); i++) {
            edit.putString(key + i, list.get(i).getId());
            edit.putString(key + i + "n", list.get(i).getName());
        }
        edit.commit();
    }

    /**
     * 获取list数据
     */
    public static ArrayList<Lot> getArrayList(Context context, String key) {
        ArrayList<Lot> list = new ArrayList<>();
        int size = getInt(context, "listSize", 0);
        for (int i = 0; i < size; i++) {
            Lot lot = new Lot();
            lot.setId(getString(context, key + i, null));
            lot.setName(getString(context, key + i + "n", null));
            list.add(lot);
        }
        return list;
    }

    //删除list
    public static void deleteList(Context context, String key) {
        SharedPreferences sp = getSp(context);
        SharedPreferences.Editor edit = sp.edit();// 获取编辑器
        int size = getInt(context, "listSize", 0);
        for (int i = 0; i < size; i++) {
            edit.remove(key + i);
            edit.remove(key + i + "n");
            edit.commit();
        }
        deleteData(context, "listSize");//删除列表玩法的缓存数据
    }

    /**
     * 设置int的缓存数据
     *
     * @param context
     * @param key     :缓存对应的key
     * @param value   :缓存对应的值
     */
    public static void setInt(Context context, String key, int value) {
        SharedPreferences sp = getSp(context);
        SharedPreferences.Editor edit = sp.edit();// 获取编辑器
        edit.putInt(key, value);
        edit.commit();
    }


    public static void setString(Context context, String key, String value) {
        SharedPreferences sp = getSp(context);
        SharedPreferences.Editor edit = sp.edit();// 获取编辑器
        edit.putString(key, value);
        edit.commit();
    }

    public static void setLong(Context context, String key, long value) {
        SharedPreferences sp = getSp(context);
        SharedPreferences.Editor edit = sp.edit();// 获取编辑器
        edit.putLong(key, value);
        edit.commit();
    }


    public static void setInt(Context context, String key, String value) {
        SharedPreferences sp = getSp(context);
        SharedPreferences.Editor edit = sp.edit();// 获取编辑器
        edit.putString(key, value);
        edit.commit();
    }

    /**
     * 删除指定key的value
     *
     * @param context
     * @param key
     */
    public static void deleteData(Context context, String key) {
        SharedPreferences sp = getSp(context);
        SharedPreferences.Editor edit = sp.edit();// 获取编辑器
        edit.remove(key);
        edit.commit();
    }

    /**
     * 清空缓存中的全部数据
     *
     * @param context
     */
    public static void clearData(Context context) {
        SharedPreferences sp = getSp(context);
        SharedPreferences.Editor editor = sp.edit();
        editor.clear().commit();
    }
}